package com.wj.tool.common.bean.restful;

import com.wj.tool.common.exception.message.ApplicationMessage;
import com.wj.tool.common.exception.message.SuccessMessage;
import org.apache.commons.lang3.StringUtils;

public final class RestfulResponseUtils {
    public static <T> RestfulResponseBody<T> successResponse() {
        RestfulResponseBody<T> response = new RestfulResponseBody();
        response.setMessage(SuccessMessage.successful().getMessage());
        response.setCode(SuccessMessage.successful().getCode());
        return response;
    }

    public static <T> RestfulResponseBody<T> successResponse(T data, String message) {
        RestfulResponseBody<T> response = new RestfulResponseBody();
        if (StringUtils.isNotEmpty(message)) {
            response.setMessage(message);
        } else {
            response.setMessage(SuccessMessage.successful().getMessage());
        }

        response.setCode(SuccessMessage.successful().getCode());
        response.setData(data);
        return response;
    }

    public static <T> RestfulResponseBody<T> errorResponse(ApplicationMessage applicationMessage) {
        RestfulResponseBody<T> response = new RestfulResponseBody();
        ResponseErrors errors = new ResponseErrors();
        errors.addError(applicationMessage.getCode(), applicationMessage.getMessage());
        response.setCode(applicationMessage.getCode());
        response.setMessage(applicationMessage.getMessage());
        response.setErrors(errors.getErrors());
        return response;
    }

    public static <T> RestfulResponseBody<T> errorResponse(ApplicationMessage applicationMessage, String... errorDetails) {
        RestfulResponseBody<T> response = new RestfulResponseBody();
        ResponseErrors errors = new ResponseErrors();
        if (null != errorDetails) {
            String[] var4 = errorDetails;
            int var5 = errorDetails.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String error = var4[var6];
                errors.addError(10001, error);
                response.setErrors(errors.getErrors());
            }
        }

        response.setCode(applicationMessage.getCode());
        response.setMessage(applicationMessage.getMessage());
        return response;
    }

    private RestfulResponseUtils() {
    }
}